<?php
namespace Home\Controller;
use Think\Controller;
class PublicController extends Controller {
	/* 版本控制 */
	public function getAppVersion(){
		$sysconfig = C('sysconfig');
		$condition['sc_id'] = $sysconfig['version'];
		$data = M("SystemConfig")->where($condition)->find();
		ApiResult(200,array('version'=>$data['sc_value'],'href'=>urlencode($data['sc_value_2'])));
	}
	/* 获取版本信息 */
	public function getVersions(){
		$versionList = M("Version")->order('ve_id asc')->select();
		ApiResult(200,array('versionList'=>$versionList));
	}
	/* 获取奖励比例 */
	public function getAwardRatio(){
		$sysconfig = C('sysconfig');
		$condition['sc_id'] = $sysconfig['other'];
		$ruleData = M("SystemConfig")->where($condition)->find();
		$rule = ($ruleData['sc_value_2']!=null&&$ruleData['sc_value_2']!='')?(int)$ruleData['sc_value_2']:0;
		ApiResult(200,array('value'=>$rule.'%'));
	}
	/* 获取扣费信息 */
	public function getPayment(){
		$sysconfig = C('sysconfig');
		$condition['sc_id'] = $sysconfig['payment'];
		$data = M("SystemConfig")->where($condition)->find();
		$result = array(
			'ordinary'=>0,
			'vip'=>0,
		);
		if(!empty($data)){
			$result['ordinary'] = $data['sc_value'];
			$result['vip'] = $data['sc_value_2'];
		}
		ApiResult(200,$result);
	}
	/* 获取系统消息 */
	public function getSysMsg(){
		$mustData = array(
			'us_id'=>$_POST['userId'],
		);
		CheckMustData($mustData);
		$condition['us_id']  = $mustData['us_id'];
		$user_info = M('User')->where($condition)->find();
		if(empty($user_info)){
			ApiResult(306);
		}else{
			if($user_info['us_is_readed']==0){
				$sysconfig = C('sysconfig');
				$condition['sc_id'] = $sysconfig['msg'];
				$msg = M("SystemConfig")->where($condition)->find();
				$result = array();
				if($msg['sc_value']!=null&&$msg['sc_value']!=''){
					$result['content'] = $msg['sc_value'];
				}
				M('User')->where($condition)->save(array('us_is_readed'=>1));
				ApiResult(200,$result);
			}else{
				ApiResult(300);
			}
		}
	}
	/* 忘记密码 */
	public function forgetPwd(){
		$mustData = array(
			'phone'=>$_POST['phone'],
			'code'=>$_POST['code'],
			'password'=>md5($_POST['password']),
		);
		CheckMustData($mustData);
		$checkCode = $this->checkCode($mustData['phone'],$mustData['code']);
		if($checkCode){
			M('User')->where(array('us_phone'=>$mustData['phone']))->save(array('us_password'=>$mustData['password']));
			ApiResult(200);
		}else{
			ApiResult(302);
		}
	}

	/* 登陆 */
	public function login(){
		$mustData = array(
			'us_phone'=>$_POST['phone'],
			'us_password'=>md5($_POST['password']),
		);
		CheckMustData($mustData);
		$user_info = M('User')->where($mustData)->find();
		if(empty($user_info)){
			ApiResult(305);
		}else{
			unset($user_info['us_password']);
			$user_info['has_tribe'] = 0;
			if($user_info['us_tribe_status']==2){
				$tribeInfo = D("Tribe")->getTribeInfoById($user_info['us_tribe_id'],$user_info['us_id']);
				$user_info['has_tribe'] = 1;
				$user_info['tribe_info'] = $tribeInfo;
			}
			
			ApiResult(200,$user_info);
		}
	}

	/* 注册 */
	public function register(){
		$mustData = array(
			'phone'=>$_POST['phone'],
			'password'=>$_POST['password'],
			'code'=>$_POST['code'],
		);
		CheckMustData($mustData);
		$condition['us_invita_code']  = array('eq', strtoupper($_POST['invita_code']));
		$user_info = M('User')->where($condition)->find();
		$where = array(
			'us_phone'=>$mustData['phone']
		);
		$checkPhone = M('User')->where($where)->find();
		if(empty($checkPhone)){
			if(empty($user_info)&&$_POST['invita_code']!=null){
				ApiResult(304);
			}else{
				$checkCode = $this->checkCode($mustData['phone'],$mustData['code']);
				if($checkCode){
					
					$sysconfig = C('sysconfig');
					$condition['sc_id'] = $sysconfig['register'];
					$sc = M("SystemConfig")->where($condition)->find();

					$user_data =array(
						'us_phone'=>$mustData['phone'],
						'us_password'=>md5($mustData['password']),
						'us_create_time'=>time(),
						'us_use_invita_code'=>$_POST['invita_code'],
						'us_invita_code'=>getRandChar(6),
						'us_award_money'=>(int)$sc['sc_value']
					);
					$user_id = M('User')->add($user_data);
					$user_info = M('User')->where(array('us_id'=>$user_id))->find();
					unset($user_info['us_password']);
					ApiResult(200,$user_info);
				}else{
					ApiResult(302);
				}
			}
		}else{
			ApiResult(303);
		}
	}

	/* 发送验证码 */
	public function send_code(){
		$mustData = array(
			'phone'=>$_POST['phone'],
			'code'=>getRandChar(4,2)
		);
		CheckMustData($mustData);
		$condition = array(
			'us_phone'=>$mustData['phone']
		);
		$user_info = M('User')->where($condition)->find();
		$result = SendSms($mustData['phone'],$mustData['code']);
		if($result){
			$code_data = array(
	    		'co_phone'=>$mustData['phone'],
	    		'co_code'=>$mustData['code'],
	    		'co_create_time'=>time()
	    	);
	    	M('Code')->add($code_data);
	        ApiResult(200);
		}else{
			ApiResult(300);
		}
	}

	public function checkCode($phone,$code){
		$now_time = time();
		$condition = array(
			'co_phone'=>$phone,
			'co_code'=>$code,
			'co_is_use'=>0
		);
		$code_info = M('Code')->where($condition)->order('co_create_time desc')->find();
		if(empty($code_info)){
			return false;
		}else{
			$time_diff = (int)$now_time-(int)$code_info['co_create_time'];
			if($time_diff/60 <= 3){
				M('code')->where(array('co_id'=>$code_info['co_id']))->save(array('co_is_use'=>1));
				return true;
			}else{
				return false;
			}
		}
	}

	/*获取html链接*/
	public function getHtmlUrlData(){
		$type = $_POST['type'];//1奖励说明的链接  2【暂时】 我的奖杯页面
		if($type==1){
			$data = array('url'=>'http://www.cocyun.cn/sm');
		}else if($type==2){
			$data = array('url'=>'http://yundailian.top/Index/trophyHtml');
		}
		
		ApiResult(200,$data);
	}
}